package com.hdax.dm.listener;

import com.hdax.dm.entity.order.DmOrder;
import com.hdax.dm.entity.scheduler.DmSchedulerSeat;
import com.hdax.dm.feign.order.DmOrderFeign;
import com.hdax.dm.feign.scheduler.DmSchedulerFeign;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RabbitListener(queues = "dm_order_dlx_queue")
@Component
public class OrderDlxListener {

    @Autowired
    private DmOrderFeign orderFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitHandler
    public void haha(String orderNo){
        //查询订单信息
        DmOrder dmOrder = orderFeign.findOrder(orderNo);
        //如果订单不存在，则终止
        if (dmOrder==null){
            return;
        }
        //如果订单已支付完成，则终止
        if (dmOrder.getOrderType()==2){
            return;
        }
        //还未支付的话，则回滚数据
        if (dmOrder.getOrderType()==0){
            rabbitTemplate.convertAndSend("tx_exchange","order_add",orderNo);
            rabbitTemplate.convertAndSend("tx_exchange","order_link_user",orderNo);
            rabbitTemplate.convertAndSend("tx_exchange","schedule_seat", orderNo);
        }
    }
}
